"""
File: example_2.py
Project: 2_统计班级成员生日情况
File Created: 2020/02/12 11:43:01
Author: Dunmore (fovd2020@outlook.com)
-----------------------------------------------------------
Description:
1.2 思考练习 基础部分 2 统计班级成员生日情况
实现班级成员生日情况的统计，输入月份数字，输出人数和名单
-----------------------------------------------------------
"""
import os
import xlrd

# 获取文件所在的路径
CURRRENT_DIR = (
    os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()
)


def read_data():
    """从Excel中读取班级成员档案信息

    Returns:
        [list]: 以excel每行数据为元素的列表，第一行为标题
    """
    data = []
    workbook = xlrd.open_workbook(os.path.join(CURRRENT_DIR, "班级成员档案.xls"))
    sheet = workbook.sheet_by_index(0)
    for i in range(sheet.nrows):
        data.append(sheet.row_values(i))
    return data


def main():
    # 读取数据
    data = read_data()
    # 分别获取姓名和生日在excel中存储的列坐标，从0开始
    name_col = data[0].index("姓名")
    birthdat_col = data[0].index("生日")

    month = input("请输入月份查询:")
    # 如果输入的是1位数，左边补0
    if len(month) == 1:
        month = "0" + month
    # 筛选出出生月份和输入月份一致的记录
    result = list(filter(lambda x: x[birthdat_col][4:6] == month, data))

    # 输出查询结果
    print(f"{int(month)}月份过生日的成员信息如下：")
    print(f"\t人数：{len(result)}")
    print(f"\t名单：{[record[name_col] for record in result]}")


if __name__ == "__main__":
    main()
